<template>
  <div class="tops">
    <el-menu
      :default-active="activeName"
      class="top"
      mode="horizontal"
      @select="handleSelect"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
    >
      <el-menu-item index="1">退出登录</el-menu-item>
      <el-menu-item index="2">修改个人信息</el-menu-item>
    </el-menu>

    <el-drawer
      title="修改个人信息"
      :visible.sync="draw"
      direction="rtl"
      size="50%"
    >
      <el-form :model="user" label-width="80px">
        <el-form-item label="昵称">
          <el-input
            v-model="user.name"
            autocomplete="off"
            width="100px"
            size="100px"
            length="100px"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-button @click="handleSure">确定修改</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script>
import stores from "@/store";
export default {
  data() {
    return {
      tel: 0, //符合规则为0，反之1
      email: 0,
      activeName: "",
      draw: false,
      user: {
        id: "",
        name: "",
        phone: "",
        email: "",
        password: "",
      },
    };
  },
  methods: {
    handleSure() {
      if(this.user.name==''){
        this.$alert("请输入昵称","提示" ,{ confirmButtonText: "我知道了" });           
      }      
      else if(this.user.phone==''){
        this.$alert("请输入电话号码","提示" ,{ confirmButtonText: "我知道了" });           
      }      
      else if(this.user.email==''){
        this.$alert("请输入邮箱","提示" ,{ confirmButtonText: "我知道了" });           
      }             
      else{
        //console.log("success")
        console.log(this.user);
        var verify1 = /^1[3456789]\d{9}$/; //手机号
        if (verify1.test(this.user.phone) || this.user.phone == "") {
          this.tel = 0;
        } else {
          this.tel = 1;
          this.$alert("修改失败,请输入正确手机号", "提示", {
            confirmButtonText: "知道了",
          });
        }
        var verify = /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/; //邮箱
        if (verify.test(this.user.email) || this.user.email == "") {
          this.email = 0;
        } else {
          this.email = 1;
          this.$alert("修改失败,请输入正确邮箱", "提示", {
            confirmButtonText: "知道了",
          });
        }
        if (this.tel == 0 && this.email == 0) {
          this.$axios
            .post(stores.spring_url + "/user/edit", this.user)
            .then((res) => {
              console.log(res);
              //修改成功
              if (res.data.code == 2001) {
                sessionStorage.setItem("id", this.user.id);
                sessionStorage.setItem("name", this.user.name);
                sessionStorage.setItem("phone", this.user.phone);
                sessionStorage.setItem("email", this.user.email);
                sessionStorage.setItem("password", this.user.password);
                this.$alert("修改成功", "提示", {
                  confirmButtonText: "知道了",
                });
                this.draw = false;
              }
              //修改失败
              else if (res.data.code == 3001) {
                this.$alert("修改失败,该昵称已存在", "提示", {
                  confirmButtonText: "知道了",
                });
              } else if (res.data.code == 4001) {
                this.$alert("修改失败,该手机号已存在", "提示", {
                  confirmButtonText: "知道了",
                });
              } else if (res.data.code == 5001) {
                this.$alert("修改失败,该邮箱已存在", "提示", {
                  confirmButtonText: "知道了",
                });
              }
            });
        }        
      } 

    },

    handleSelect(key, keyPath) {
      this.user.id = window.sessionStorage.getItem("id");
      this.user.name = window.sessionStorage.getItem("name");
      this.user.phone = window.sessionStorage.getItem("phone");
      this.user.email = window.sessionStorage.getItem("email");
      this.user.password = window.sessionStorage.getItem("password");
      //console.log(this.activeName);
      //console.log(key);
      //退出登录时需要清空数据
      if (key == 1) {
        sessionStorage.setItem("id", "");
        sessionStorage.setItem("name", "");
        sessionStorage.setItem("phone", "");
        sessionStorage.setItem("email", "");
        sessionStorage.setItem("password", "");
        this.$router.push("/login");
      } else if (key == 2) {
        this.draw = true;
      }
    },
  },
};
</script>
<style>
.top {
  margin: 0px;
  padding: 0;
}
</style>